package com.carrot.newsgallery.common;

import com.carrot.newsgallery.utils.MyDatabaseException;
import com.carrot.newsgallery.utils.MyLoginException;
import com.carrot.newsgallery.utils.ReturnCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLSyntaxErrorException;

@RestControllerAdvice
public class ProjectExceptionHandler {

    @ExceptionHandler(MyLoginException.class)
    public R loginException(MyLoginException exception){
        //打印输出日志信息
//        exception.printStackTrace();
        return R.fail(ReturnCode.USER_NOT_LOGIN,"您尚未登陆,无权限访问该内容!");
    }

    @ExceptionHandler(MyDatabaseException.class)
    public R databaseException(MyDatabaseException exception){
        exception.printStackTrace();
        if(exception.getStatus()==MyDatabaseException.errorCode.FAILURE)
            return R.fail(ReturnCode.SERVER_ERROR,"服务器发生异常,请稍后重试!");
        else
            return R.fail(ReturnCode.SERVER_ERROR,"未知错误,请稍后重试");
    }

//    @ExceptionHandler(IOException.class)
//    public R ioException(IOException exception){
//        return R.fail(ReturnCode.SERVER_ERROR,"服务器发生异常,请稍后再试");
//    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    public R sqlSyntaxErrorException(SQLSyntaxErrorException exception){
        return R.fail(ReturnCode.SERVER_ERROR,"数据查询异常,请稍后充实");
    }

}
